# Maintainer: Jelle van der Waa <jelle@archlinux.org>

pkgname=alertmanager
pkgver=0.26.0
pkgrel=1
pkgdesc="Service which handles alerts sent by client applications such as the Prometheus server"
url="https://github.com/prometheus/alertmanager"
arch=(loong64 x86_64)
depends=(glibc)
license=('Apache')
makedepends=(go git)
options=(!lto)
backup=('etc/alertmanager/alertmanager.yml' 'etc/conf.d/alertmanager')
source=($pkgname-$pkgver.tar.gz::https://github.com/prometheus/alertmanager/archive/v${pkgver}.tar.gz
        alertmanager.service
        alertmanager.sysusers
        alertmanager.conf)
sha512sums=('b5568f0cebe487b5c81b56febad30bbe84918ed0e3e8be2ecd7820d13d7c5373b17e00238a9817072c59040a5618307811280ac00ef6efa3e3439f9f3378f464'
            'b16f14f0a1ff6462f263cfd10e62509adc2dbebdd10fa31d240892619fbef17dc593a33f0fbd0f32adb43d565f676fdc90bbd69a3a447d2a46deeef5f9916ae4'
            '469f321f40b0dd6e1cc6d0791032c476449bb2ab2364d57b06d0e0309d09710be8751ded64d84e29dd6e28e96b71ef69e2bee6c71282500a9074a9d7ada8bdf0'
            'dcb3d8a29e705c39b13cc8b876975ff1af88e86ba6d76c524aeba188acd2f97fea0278632bee7fd5f66cc0fe780aa9f4a6a98696025ca84c767eb5eae831de2f')

build() {
  cd $pkgname-$pkgver
  go build \
    -trimpath \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-extldflags ${LDFLAGS} \
      -X github.com/prometheus/common/version.Version=$pkgver \
      -X github.com/prometheus/common/version.Revision=$pkgver \
      -X github.com/prometheus/common/version.Branch=tarball \
      -X github.com/prometheus/common/version.BuildUser=someone@builder \
      -X github.com/prometheus/common/version.BuildDate=$(date -d@"$SOURCE_DATE_EPOCH" +%Y%m%d-%H:%M:%S)" \
    ./cmd/alertmanager

  go build \
    -trimpath \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-extldflags ${LDFLAGS} \
      -X github.com/prometheus/common/version.Version=$pkgver \
      -X github.com/prometheus/common/version.Revision=$pkgver \
      -X github.com/prometheus/common/version.Branch=tarball \
      -X github.com/prometheus/common/version.BuildUser=someone@builder \
      -X github.com/prometheus/common/version.BuildDate=$(date -d@"$SOURCE_DATE_EPOCH" +%Y%m%d-%H:%M:%S)" \
    ./cmd/amtool
}

package() {
  cd $pkgname-$pkgver
  install -Dm755 $pkgname "$pkgdir/usr/bin/$pkgname"
  install -Dm755 amtool "$pkgdir/usr/bin/amtool"

  install -Dm644 examples/ha/${pkgname}.yml "${pkgdir}/etc/${pkgname}/${pkgname}.yml"
  install -d -m755 "${pkgdir}/etc/${pkgname}/template/"

  install -d -m750 -o 211 -g 211 "${pkgdir}/var/lib/${pkgname}"

  install -Dm0644 "${srcdir}/${pkgname}.service" "${pkgdir}/usr/lib/systemd/system/${pkgname}.service"
  install -Dm644 "$srcdir/alertmanager.sysusers" "$pkgdir/usr/lib/sysusers.d/alertmanager.conf"
  install -Dm644 "$srcdir/alertmanager.conf" "$pkgdir/etc/conf.d/alertmanager"
}
